package com.yjd.job.shettlement;

import com.alibaba.edas.schedulerX.ProcessResult;
import com.alibaba.edas.schedulerX.ScxSimpleJobContext;
import com.alibaba.edas.schedulerX.ScxSimpleJobProcessor;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.settlement.service.IClearDayFundChannelService;
import com.yjd.comm.settlement.service.IClearDayPfService;
import com.yjd.comm.settlement.service.IMerchantSettlementService;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 日结算job
 * Created by yjd-wap on 2017/11/15.
 */
public class ClearDayDetailJob implements ScxSimpleJobProcessor {
    Logger logger = Logger.getLogger(ClearDayDetailJob.class);

    @Resource
    private IMerchantSettlementService merchantSettlementService;

    @Resource
    private IClearDayPfService clearDayPfService;

    @Resource
    private IOrderService orderService;

    @Resource
    private IClearDayFundChannelService clearDayFundChannelService;

    @Override
    public ProcessResult process(ScxSimpleJobContext scxSimpleJobContext) {
        // 当前时间（秒数cc）
        Long currtime = FrameUtil.getTime();
        // 前一天（秒数）
        Long previousTime = currtime - DateUtil.DAY_IN_SECOND;
        // 前一天的日期（yyyyMMdd），如：20170824
        int previousDate = Integer.valueOf(DateUtil.format(new Date(previousTime * 1000), DateUtil.PATTERN_yyyyMMdd));

        logger.info("日结算job，清算日期：" + previousDate + "清算开始");
        try {
            Long channelDate = DateUtil.getCurrDateFristSecond()-86400;
            // 日结明细
            orderService.settlement(previousTime);

            // 商户日结
            merchantSettlementService.settlementAll(previousTime);

            // 资金渠道日清算
            clearDayFundChannelService.settlementAll(channelDate,channelDate);

            // 平台日结
           // clearDayPfService.generatePfClearDayData(previousDate, FrameUtil.getTime());



        } catch (Exception e) {
            this.logger.info("生成日期结算信息失败，错误信息：" + e);
        }
        logger.info("日结算job，清算日期：" + previousDate + "清算结束");
        return new ProcessResult(true);
    }
}
